var $path=$("#webPath").val();
var hostid = $("#hostid").val();
$(function() {
	initDate();
	
	$(".press_btn1").click(function() {
		jump(1);
	});
	$("#applicationid").change(function() {
		showItems();
	});
});

function initDate() {
	var date = new Date();
	//默认展示3个小时的数据
	date.setHours(date.getHours()-3);
	$("#startDate").val(formatDateTime(date));
	$("#endDate").val(formatDateTime(new Date()));
	$("#pageMenu").html("");
}

function showItems() {
	var applicationid = $("#applicationid > option:selected").val();
	if(applicationid==0){
		var itemid = $("#itemid").html("");
		itemid.append("<option value='0'>请先选择监控项</option>");
		return;
	}
	$.ajax({
		url:$path+"/radar/host/showItems.do?",
		type:"post",
		dataType:"json",
		data:{
			"applicationid":applicationid
		},
		success:function(result){
			if (result.code == '200') {
				var itemList = result.data;
				var itemid = $("#itemid").html("");
				for(var i=0, len=itemList.length;i<len;i++){
					itemid.append("<option value='"+itemList[i].itemid+"'>"+itemList[i].key_+"</option>");
				}
			}
		}
	})
}

//根据历史数据绘制图表
function showDataImg(pageList) {
	
	var myChart = echarts.init(document.getElementById("dataImg"));
	var time = new Array();
	var value = new Array();
	var ns = new Array();
	for(var i = 0, len=pageList.length;i<len;i++){
		time[i]=pageList[i].dateStr;
		value[i]=pageList[i].value;
		ns[i]=pageList[i].ns/1000000000;
	};
	var option = {
		title :{
			text:"历史数据表"
		},
		tooltip:{
			trigger:"axis"
		},
		legend:{
			data:["监控项数据值","用时/秒"]
		},
	    grid: {
	        left: '3%',
	        right: '4%',
	        bottom: '3%',
	        containLabel: true
	    },
	    toolbox: {
	        feature: {
	            saveAsImage: {}
	        }
	    },
	    xAxis: {
	        type: 'category',
	        boundaryGap: false,
	        data: time
	    },
	    yAxis: {
	        type: 'value'
	    },
	    series: [
	         {
	             name:'监控项数据值',
	             type:'line',
	             stack: '总量',
	             data:value
	         },
	         {
	             name:'用时/秒',
	             type:'line',
	             stack: '总量',
	             data:ns
	         }
	    ]
	}
	myChart.setOption(option);
}

// 改变页面大小
function changePageSize(pageNo) {
	jump(1);
}

function jump(pageNo) {
	var startDate = $("#startDate").val().trim();
	var endDate = $("#endDate").val().trim();
	var itemid = $("#itemid > option:selected").val();
	$("#historyList").html("");
	$("#pageMenu").html("");
	$.ajax({
				url : $path+"/radar/host/getHistorys.do",
				type : "POST",
				dataType:"json",
				data : {
					"itemid":itemid,
					"startDate" : startDate,
					"endDate" : endDate
				},
				success : function(result) {
					if (result.code == '201') {
						layer.msg("数据为空",{time:2000});
						$("#dataImg").html("");
						$("#pageTitle").html("");
						$("#pageTable").html("");
						$("#pageMenu").html("");
					}else if (result.code == '200') {
						layer.msg("正在查询，请稍后",{time:800});
						pageData = result.pageData;
						//绘制图形
						pageList = pageData.pageList;
						showDataImg(pageList);
						unifiedPaging(pageData);
					}
				}
			});
}